package soar.test.jpa.check;

import java.time.Instant;
import java.util.Date;

import javax.persistence.EntityManager;

import soar.test.jpa.share.Resource;
import soar.test.jpa.share.entity.mysql.TestEntity;

class MySQL
{
    public static void testEntity(String persistence_unit) throws Exception
    {
        EntityManager em = null;
        try
        {
            em = Resource.getInstance()
                    .getEntityManagerHandler(persistence_unit)
                    .createEntityManager();

            em.getTransaction().begin();

            TestEntity test_entity = new TestEntity();
            test_entity.setC_datetime(new Date());
            test_entity.setC_varchar(Instant.now().toEpochMilli() + "");
            test_entity.setC_char(Instant.now().toString());
            test_entity.setC_tinyint1(10);
            em.persist(test_entity);

            em.getTransaction().commit();
        }
        catch (Exception e)
        {
            System.err.println("fail: " + e);
            try
            {
                if (em != null && em.getTransaction().isActive())
                    em.getTransaction().rollback();
            }
            catch (Exception er)
            {
                System.err.println("rollback fail: " + er);

            }
        }
        finally
        {
            if (em != null)
                em.close();
        }
    }
}
